<template name="userAddress">
  <view class="container">
    <scroll-view class="address-list">
      <view
        v-for="(address, index) in addresses"
        :key="address.id"
        :class="['address-item', { selected: selectedAddress === address.id }]"
        @tap="selectAddress(address)"
      >
        <view class="address-info">
          <text>{{ address.name }}</text>
          <text>{{ address.phone }}</text>
          <text>{{ address.detail }}</text>
        </view>
        <view class="address-phone">
          <button class="edit-btn" @tap.stop="editPhone(index)">✏️</button>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      addresses: [
        { id: 1, name: '刘硕', phone: '15630213975', detail: '北京北京市丰台区花乡街道广安康润家园东区' },
        { id: 2, name: 'xiaoyu', phone: '15176790', detail: '北京北京市大兴区111' },
        { id: 3, name: '小猫', phone: '13031586860', detail: '北京北京市丰台区花乡街道广安康润家园东区' }
      ],
      selectedAddress: null,
    };
  },
  methods: {
    // 选择地址
    selectAddress(address) {
      uni.$emit('addressSelected', address);// 发送选中的地址数据
      uni.navigateBack();// 返回上一个页面
    },
  },
};
</script>

<style lang="scss">
.container {
  padding: 20px;
}

.address-list {
  list-style: none;
  padding: 0;
}

.address-item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ddd;
  cursor: pointer;
  border-radius: 5px;
  transition: background-color 0.3s ease;
}

.address-item.selected {
  background-color: #e0f7fa;
}

.address-info {
  display: flex;
  flex-direction: column;
}

.address-postcode {
  display: flex;
  align-items: center;
}

.edit-btn {
  margin-left: 2px;
  background: none;
  border: none;
  cursor: pointer;
}
</style>